using UnityEditor;
using UnityEngine;
using System.IO;
using System.Collections.Generic;
using System;

public class LoadSpellsFromCSV : ScriptableWizard {
    public static string dbURI;
    
    [MenuItem ("Load JSON/Load Spells From CSV")]
    static void CreateWizard () {
        dbURI = EditorUtility.OpenFilePanel(
                "Load from CSV",
                "",
                "csv");
		Debug.Log(dbURI);
		
		SpellManager.resetManager();
		
		using (StreamReader readFile = new StreamReader(dbURI))
		{
			string line;
			string[] row;
			
			readFile.ReadLine();
			
			while ((line = readFile.ReadLine()) != null)
			{
				row = line.Split('~');
				
				List<string> spellList = new List<string>();
				List<int> spellLevel = new List<int>();
				
				string description = "";
				
				if(!string.IsNullOrEmpty(row[7]))
					description += "Area: " + row[7] + "\n\n";
				
				if(!string.IsNullOrEmpty(row[8]))
					description += "Effect: " + row[8] + "\n\n";
				
				if(!string.IsNullOrEmpty(row[9]))
					description += "Target: " + row[9] + "\n\n";
				
				description += row[13];
				
				for(int i = 16; i < 22; i++)
				{
					if(row[i] != "NULL")
					{
						if(i == 16)
							spellList.Add("Sorceror/Wizard");
						else if(i == 17)
							spellList.Add("Cleric");
						else if(i == 18)
							spellList.Add("Druid");
						else if(i == 19)
							spellList.Add("Ranger");
						else if(i == 20)
							spellList.Add("Bard");
						else
							spellList.Add("Paladin");
						
						spellLevel.Add(int.Parse(row[i]));
					}
				}
				
				SpellManager.AddSpell(row[0], row[1] + (string.IsNullOrEmpty(row[2])?"":"("+row[2]+")") + (string.IsNullOrEmpty(row[3])?"":"["+row[3]+"]"),
					row[4], row[5], row[6], row[10], row[11], (row[12] != "no"), spellList, spellLevel, description, false);
			}
		}
			
    }
    void OnWizardCreate () {
 		
    } 
}